<?php
/**
 * @author: liaofei<136327134@qq.com>
 * @day: 2021/6/8
 */

namespace App\Services\user;

use App\Dao\user\UserInfoDao;

use App\Models\system\SystemAdminModel;
use App\Services\BaseServices;
use App\Dao\system\SystemAdminDao;
use App\Exceptions\ApiException as HOException;
use Hlcreation\Basic\Helper;
use App\Services\system\SystemLogServices;

/**
 * Class SystemRoleServices
 * @package app\services\user
 */
class UserInfoServices extends BaseServices
{



    /**
     * UserInfoServices constructor.
     * @param SystemRoleDao $dao
     */
    public function __construct(UserInfoDao $dao)
    {
        $this->dao = $dao;
    }


    /**
     * 代理商列表
     * @param string $account
     */
    public function UserList(array $where,$post)
    {

       /* if (!empty($where['create_time'])){
            $nowdate = date("Y-m-d");
            $sevendate = date("Y-m-d", strtotime(" -7 day"));
            $between =  !empty($where['create_time']) ? [$where['create_time']." 00:00:00", $where['create_time']." 23:59:59"] : [$sevendate." 00:00:00", $nowdate." 23:59:59"];
            $where['nowdate']=$nowdate;
            $where['sevendate']=$sevendate;
            $where['between']=$between;
        }*/

        return  $this->dao->UserList($where,$post);

    }


    /**
     * 组合数据
     */
    private function  CombinedData(array $data=[],string $str=''){

        //账号信息
        $user['created_at'] =date('Y-m-d H:i:s');
        $user['expiration_time']= empty($data['expiration_time'])?0:strtotime($data['expiration_time']);
        $user['account']=$data['account'];
        $user['halt']=Helper::createNonceStr(5);
        $user['pwd']=Helper::returnPwd($data['password'], $user['halt']);
        $user['roles']=2;
        $user['level']=1;
        $user['status']=1;
        if($str=='StaffUpdate')  $user['creator_uid']=$data['creator_uid'];
        if($str=='StaffSave')  $user['creator_uid']=$data['creator_uid'];

        $user['it_disabled']=0;
        $user['store_num']=$data['store_num'];
        $user['real_name']=$data['staff_name'];
        //员工信息
        $staff['created_at']=date('Y-m-d H:i:s');
        $staff['staff_name']=$data['staff_name'];
        $staff['phone']=$data['phone'];

        return compact('user','staff');
    }


    /**
     *
     *   添加员工
     *
     * */
    public function StaffSave($arr){

        $systemAdminServices = app()->make(SystemAdminDao::class);
        if($systemAdminServices->count(['account'=>$arr['account']])) throw new HOException('账号已存在');
        if(empty($arr['real_name']))  throw new HOException('真实姓名不能为空');
       // $data=$this->CombinedData($arr,'StaffSave');

        //dd($data);
        $arr['pwd'] = md5($arr['pwd']);
        $arr['level'] = 1; //员工
        return  $this->dao->StaffSave($arr);
    }


    /**
     * 查看员工信息
     * */
    public function getStaff(string $sraff_id=''){
        if($sraff_id=='') throw new HOException('员工id不能为空');
        return  $this->dao->getStaff(['id'=>$sraff_id]);
    }




    /**
     * 禁用员工
     * */
    public  function  editDisabled(int $admin_id,string $user_id,int $tatus){
        if($user_id=='') throw new HOException('账号id不能为空');
        $systemAdminServices = app()->make(SystemAdminDao::class);

        if($systemAdminServices->update(['id'=>$user_id],['it_disabled'=>$tatus])){
            //插入日志
            $type=empty($tatus)?'关闭代理商':'禁用代理商';
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($user_id,'禁用代理商','代理商账号id:'.$user_id.'操作:'.$tatus);
            return true;
        }else{
            return false;
        }

    }


    /**
     * 编辑员工
     * */
    public function StaffUpdate(string $staff_id,array $arr){
        if($staff_id=='') throw new HOException('员工id获取账号id不能为空');
        $systemAdminServices = app()->make(SystemAdminDao::class);
        if($systemAdminServices->IsAccount($arr['account'],$staff_id)) throw new HOException('账号已存在');
       /* $data=$this->CombinedData($arr,'StaffUpdate');*/
        if(empty($arr['pwd'])){
            $arr['pwd'] = SystemAdminModel::where('id',$staff_id)->value('pwd');
        }else{
            $arr['pwd'] = md5($arr['pwd']);
        }
       unset($arr['staff_id']);
        return  $this->dao->StaffUpdate($staff_id,$arr);
    }


    /**
     *  删除员工
     * */
    public function StaffDelte(int $admin_id,string $staff_id){
        if($staff_id=='') throw new HOException('员工id不能为空');
        return  $this->dao->staffDelte($admin_id,$staff_id);
    }






}
